package com.wsd.serviceImpl;

import com.wsd.constant.RepaymentPlanStatusEnum;
import com.wsd.dao.LoanInformationMapper;
import com.wsd.dao.RepaymentPlanMapper;
import com.wsd.domain.Loan;
import com.wsd.domain.LoanInformation;
import com.wsd.domain.RepaymentPlan;
import com.wsd.service.RepaymentPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RepaymentPlanServiceImpl implements RepaymentPlanService {

    @Autowired
    private RepaymentPlanMapper repaymentPlanMapper;
    @Autowired
    private LoanInformationMapper loanInformationMapper;


    @Override
    public List<RepaymentPlan> selectRepaymentPlanByLoanId(long loanId) {
        return repaymentPlanMapper.selectRepaymentPlanByLoanId(loanId);
    }

    /**
     * 根据订单编号查询某次贷款的所有还款记录详细
     *
     * @param loanId
     * @return
     */
    @Override
    public List<RepaymentPlan> selectRepaymentPlanByLoanIdAndStatus(String loanId) {
        long id = Long.parseLong(loanId);
        return repaymentPlanMapper.selectRepaymentPlanByLoanIdAndStatus(id);
    }
    @Override
    public List<RepaymentPlan> selectCurrentRepaymentPlan(long customerId){
        List<Long> loanIds=loanInformationMapper.selectLoanIdByCustomerId(customerId);
        //获取用户多笔贷款还款计划，并筛出最近未还款信息
        List<List<RepaymentPlan>> repaymentPlansList =new ArrayList<>();
        List<RepaymentPlan> repaymentPlans=new ArrayList<>();
        for(Long loanId:loanIds){
            repaymentPlansList.add(repaymentPlanMapper.selectRepaymentPlanByLoanId(loanId));
        }
        for(List<RepaymentPlan> list:repaymentPlansList){
            for(RepaymentPlan repaymentPlan:list){
                if(RepaymentPlanStatusEnum.NOT_REPAYMENT.getType().equals(repaymentPlan.getRepaymentStatus())){
                    repaymentPlans.add(repaymentPlan);
                    break;
                }
            }
        }
        return repaymentPlans;
    }
}
